﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using PhoneApp1.Resources;
using System.IO;
using System.Xml.Linq;
using HtmlAgilityPack;
using System.Text.RegularExpressions;
namespace PhoneApp1
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (!TextBlockKeyword.Text.Equals(""))
            {
                System.Uri targetUri = MakeUrirequest();
                WebClient client = new WebClient();
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
                client.DownloadStringAsync(targetUri);
            }
            else
            {
                TextBlockResults.Text = "Hãy nhập từ cần tìm kiếm";
            }
        }

        private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            HtmlDocument document = new HtmlDocument();
            try
            {
                document.LoadHtml(e.Result);
                HtmlNode divNode = document.DocumentNode.SelectSingleNode("//div[@class='pronounce']");
                HtmlNodeCollection scriptsnode = document.DocumentNode.SelectNodes("//script[@type='text/javascript'] ");
                List<HtmlNode> ListNode = new List<HtmlNode>();
                foreach (HtmlNode node in scriptsnode)
                {
                    ListNode.Add(node);

                }


                HtmlNode Node = ListNode.ElementAt(7);


                //if (Node != null)
                //    TextBlockResults.Text = Node.InnerText;
                //else
                //    TextBlockResults.Text = "Không tìm thấy";

                string pattern = " |,|\" ";
                Regex myRegex = new Regex(pattern);
                string[] sKetQua = myRegex.Split(Node.InnerText);
                TextBlockResults.Text = "";
                foreach (string subString in sKetQua)
                {
                    if (Regex.IsMatch(subString, @"(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?(.mp3)"))
                    {

                        TextBlockResults.Text = subString.Substring(1, subString.Length - 2)+ "\n";
                    }
                }
            }
            catch (Exception ex)
            {
                TextBlockResults.Text = ex.Message;
            }
   
            }


        private Uri MakeUrirequest()
        {
           
    
                String word = TextBlockKeyword.Text;
                System.Uri targetUri = new System.Uri(string.Format("http://vdict.com/{0},1,0,0.html", word));
                return targetUri;

            
           
        }   

        
    }
}